// manager/pages/process/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        currentComponent: '',
        stepList: [
            {
            	id: 1,
            	text: "基本信息",
            	component: "basicinfo",
            	success: false,
            	selected: false
            },
            {
            	id: 2,
            	text: "客户信息",
            	component: "custinfo",
            	success: false,
            	selected: false
            },
            {
            	id: 3,
            	text: "家庭成员",
            	component: "familyInfo",
            	success: false,
            	selected: false
            },
            {
            	id: 4,
            	text: "影像资料",
            	component: "imageData",
            	success: false,
            	selected: false
            },
            {
            	id: 5,
            	text: "影像资料",
            	component: "imageData",
            	success: false,
            	selected: false
            },
            {
            	id: 6,
            	text: "影像资料",
            	component: "imageData",
            	success: false,
            	selected: false
            }
        ],
        readonly: false,
        dicts: {},
        basicinfo: {
            id: "",
            locationName: "",
            locationAbbreviation: "",
            locationPic: "",
            locationCoordinate: "",
            locationAddress: "",
            locationLayers: "",
            locationContacts: "",
            locationPhone: "",
            locationType: 1,
            remark: "",
        },
        custinfo: {},



        // 审批意见
        showOpinion: false,
        opinionForm: {
            approveType: "",
            approveMemo: "",
        },
        // 下一节点审批人
        showNode: false,
        approvePop: 3,
        stepName: "",
        checkedUserId: "",
        userList: [
            {
                userId: "1",
                checked: false,
                userName: "周三"
            },
            {
                userId: "2",
                checked: false,
                userName: "赵琦琦"
            },
            {
                userId: "3",
                checked: false,
                userName: "钱报"
            },
            {
                userId: "4",
                checked: false,
                userName: "孙立"
            },
        ]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /** 切换 */
    stepChange(e){
        let { index } = e.detail;
        let { currentComponent, stepList } = this.data;
        if(currentComponent != stepList[index].component){
            this.setStepSeleced(index);
        }
        var basicinfo = this.selectComponent('#basicinfo')
        // var custinfo = this.selectComponent('#custinfo')
        console.log(basicinfo.validationForm())
        // console.log(custinfo.s())
    },
    /** 检查是否全部点击查看 */
    isAllClick(){
        let { stepList } = this.data;
        const item = stepList.find(item => item.isClick == false);
        if(!item){
            return true
        }else{
            wx.showToast({
                title: item.text + '页签需要审阅！', 
                icon: "none"
            });
            return false
        }
    },
    /** 检查是否全部填完信息 */
    isAllSuccess(){
        let { stepList } = this.data;
        const item = stepList.find(item => item.success == false);
        if(!item){
            return true
        }else{
            // if(item.component == 'applyInfo'){
            //     if(this.$refs.applyInfo.validationForm()){
            //         wx.showToast({
            //             title: '请添加' + item.text + '！', 
            //             icon: "none"
            //         });
            //     } 
            //     if(this.applyInfo.authSts == 0){
            //         wx.showToast({
            //             title: '请完成' + item.text + '授权！', 
            //             icon: "none"
            //         });
            //     }
            // }else{
            //     wx.showToast({
            //         title: '请添加' + item.text + '！', 
            //         icon: "none"
            //     });
            // }
            return false
        }
    },
    /** 设置选中 */
    setStepSeleced(index){
        let { currentComponent, stepList } = this.data;
        stepList.map((item,i) => {
            if(index == i){
                currentComponent = stepList[index].component;
                item.isClick = true;
                item.selected = true;
            }else{
                item.selected = false;
            }
        })
        this.setData({
            currentComponent, 
            stepList
        })
    },
    /** 设置完成 */
    setStepSuccess(component){
        let { stepList } = this.data;
        stepList.map((item,i) => {
            if(component == item.component){
                item.success = true;
            }
        })
        this.setData({
            stepList
        })
    },
    /** 设置未完成 */
    setStepFail(component){
        let { stepList } = this.data;
        stepList.map((item,i) => {
            if(component == item.component){
                item.success = false;
            }
        })
        this.setData({
            stepList
        })
    },
    /** 设置下一个Component */
    setNextComponent(){
        let { currentComponent, stepList } = this.data;
        let index = stepList.findIndex(item => item.component == currentComponent);
        if(index < stepList.length - 1){
            this.setStepSeleced(index+1);
        }
    },
    /** 设置当前组件请求完成 */
    setComponentHideLoading(component){
        let { stepList } = this.data;
        stepList.map((item,i) => {
            if(component == item.component){
                item.showLoading = false;
            }
        })
        this.setData({
            stepList
        })
    },

    /** 保存基本信息 */
    saveBasicinfo(e){
        console.log(e.detail)
        this.setData({
            basicinfo: e.detail
        })
    },


    /** 提交意见输入框输入时 */
    opinionInput(e){
        let key = e.target.dataset.key;
        this.setData({
            ['opinionForm.'+key]: e.detail.value
        })   
    },

    /** 提交意见选择框变化时 */
    opinionPickerChagne(e){
        let { key, value } = e.detail;
        this.setData({
            // array[e.detail.value].approvCode + (array[e.detail.value].returnStepId || '');
            ['opinionForm.'+key]: value
        })
    },
    /** 提交意见取消 */
    cancelOpinion(){
        this.setData({
            showOpinion: false
        })
    },
    /** 提交意见确认 */
    confirmOpinion(){
        let { opinionForm } = this.data;
        //表单校验
        if (!opinionForm.approveType) {
            wx.showToast({ title: "请选择审批意见!", icon: "none" });
            return
        }
        if (opinionForm.approveType != 'T' && opinionForm.approveMemo == '') {//如果选择了拒件，拒件理由为必填
            wx.showToast({ title: "请输入意见说明!", icon: "none" });
            return;
        }
        this.setData({
            showOpinion: false
        })
    },


    /** 选择下一节点审批人 */
    onUserIdCheckboxChange(e) {
        console.log(e)
        let { key, item } = e.currentTarget.dataset;
        let arr = this.data[key]?this.data[key].split(","):[];
        console.log(arr)
        console.log(arr.indexOf(item.userId))
        let index = arr.indexOf(item.userId);
        if(index != -1){
            arr.splice(index,1);
        }else{
            arr.push(item.userId);
        }
        this.setData({
            [key]: arr.join(",")
        });
    },
    /** 下一节点审批人取消 */
    cancelNode(){
        this.setData({
            showNode: false
        })
    },
    /** 下一节点审批人确认 */
    confirmNode(){
        let { approvePop, checkedUserId } = this.data;
        let checkedUserIdArr =  checkedUserId?checkedUserId.split(","):[];
        //表单校验
        if (checkedUserIdArr.length == 0) {
            wx.showToast({ title: "请选择下一节点审批人!", icon: "none" });
            return
        }
        if (checkedUserIdArr.length < approvePop) {
            wx.showToast({ title: `下一节点需要${approvePop}人审批!`, icon: "none" });
            return;
        }
        // 向父窗体传递参数
        // uni.$emit('flownodesave',this.checkedArray);
        this.setData({
            showNode: false
        })
    },
})